package com.jushu.storbox.view.resolve;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jushu.storbox.R;
import com.jushu.storbox.adapter.ListDisplayResolveAdapter;
import com.jushu.storbox.operation.group.BasicItemOperation;
import com.jushu.storbox.util.LogUtil;

/**
 * this class encapsulates PullToRefreshListView {@link PullToRefreshListView}</br>
 * and imort (not implements) basic operation such as: </br>
 * 1.adapter's operation </br>
 * 2.freshList's operation pullup.. and pulldown.. {@link PullToRefreshBase.OnRefreshListener2} </br>
 * 3.itemclick event {@link OnItemClickListener} </br>
 * 
 * @param <AdapterItemDataType> the data, the adapter apply to the UI
 * @param <ItemOperationType> how to operation the item
 * @param <AdapterType> adapter's type must be sub class of PageListAdapter {@link ListDisplayResolveAdapter}
 */
public abstract class ListDisplayResolve<AdapterItemDataType, 
									  ItemOperationType extends BasicItemOperation<AdapterItemDataType>, 
									  AdapterType extends ListDisplayResolveAdapter<AdapterItemDataType, ItemOperationType>> 
				implements OnItemClickListener, PullToRefreshBase.OnRefreshListener2<ListView> {
	
	private static final String TAG = ListDisplayResolve.class.getSimpleName();

	protected Context context;
	
	protected PullToRefreshListView detailList = null;
	protected AdapterType adapter;
	private ItemOperationType operation;
	private boolean isRefreshing = false;
	
	public ListDisplayResolve(Context context) {
		this.context = context;
	}
	
	public PullToRefreshListView getComponent(){
		return detailList;
	}
	
	public void initComponent(PullToRefreshListView pullToRefreshListView){
		detailList = pullToRefreshListView;
		detailList.setMode(Mode.BOTH);
		
		ILoadingLayout startLabels = detailList.getLoadingLayoutProxy(true, false);
		startLabels.setPullLabel(context.getResources().getString(R.string.pull_to_refresh_pull_label));
		startLabels.setRefreshingLabel(context.getResources().getString(R.string.pull_to_refresh_refreshing_label));
		startLabels.setReleaseLabel(context.getResources().getString(R.string.pull_to_refresh_release_label));
		ILoadingLayout endLabels = detailList.getLoadingLayoutProxy(false, true);
		endLabels.setPullLabel(context.getResources().getString(R.string.pull_to_refresh_from_bottom_pull_label));
		endLabels.setRefreshingLabel(context.getResources().getString(R.string.pull_to_refresh_from_bottom_refreshing_label));
		endLabels.setReleaseLabel(context.getResources().getString(R.string.pull_to_refresh_from_bottom_release_label));
	}
	
	public void binddingAdapter() {
		if(adapter == null) { 
			adapter = initTemplateAdapter();
		}
		if(operation == null) {
			operation = initAdapterOperation();
		}
		if(detailList != null) {
			detailList.setOnItemClickListener(this);
			detailList.setOnRefreshListener(this);
			if(adapter != null) {
				adapter.setOperation(operation);
				detailList.setAdapter(adapter);
			}
		}
		if(adapter.getCount() <= 0) {
			loadDataList();
		}	
	}
	
	protected abstract AdapterType initTemplateAdapter();
	
	protected abstract ItemOperationType initAdapterOperation();

	public void addData(int index, AdapterItemDataType data){
		adapter.addData(index, data);
	}
	
	public void addData(AdapterItemDataType data){
		adapter.addData(data);
	}
	
	public void addDatas(List<AdapterItemDataType> datas){
		adapter.addDatas(datas);
	}
	public void resetDatas(List<AdapterItemDataType> datas) {
		adapter.resetDatas(datas);
	}
	
	public void disableMultiSelect(){
		adapter.setMultiMode(false);
		adapter.notifyDataSetChanged();
	}
	
	public void enableMultiSelect(){
		adapter.setMultiMode(true);
		adapter.notifyDataSetChanged();
	}
	
	@Override
	public final void onItemClick(AdapterView<?> adapterView, View clickView, int position, long id) {
		@SuppressWarnings("unchecked")
		AdapterItemDataType data = (AdapterItemDataType) adapterView.getAdapter().getItem(position);
		clickItem(clickView, position, id, data);
	}

	@Override
	public final void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		if(!isRefreshing()) {
			setRefreshing(true);
			refreshDataList();
		}
	}

	@Override
	public final void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		if(!isRefreshing()) {
			setRefreshing(true);
			getMoreDataList();
		}
	}
	
	public final void onRefreshComplete() {
		setRefreshing(false);
		if(detailList != null) {
			detailList.onRefreshComplete();
		} else {
			LogUtil.e(TAG, "the instance of detailList is null!");
		}
		doOnRefreshComplete();
	}
	
	protected void doOnRefreshComplete() {
	}
	
	protected void loadDataList() {
	}

	protected void refreshDataList() {
		//temp call this mothed
		loadDataList();
	}
	
	protected void getMoreDataList() {
	}
	
	protected void clickItem(View clickView, int position, long id, AdapterItemDataType data) {
	}
	
	protected void requestActivityForResult(Intent intent) {}
	
	public void onActivityResult(int responseCode, Intent data) {
		LogUtil.i(TAG, "USAGE: use default function proc onActivityResult");
	}
	
	public AdapterType getAdapter() {
		if(adapter == null){
			adapter = initTemplateAdapter();
		}
		return adapter;
	}

	boolean isRefreshing() {
		return isRefreshing;
	}

	void setRefreshing(boolean isRefreshing) {
		this.isRefreshing = isRefreshing;
	}
}


